using System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class UserAdministration : System.Web.UI.Page
{
    #region Handlers

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            hfSearchQuery.Value = "%";
            repAlphabet.DataSource = GetAlphabetList();
            repAlphabet.DataBind();
        }
    }

	protected void lbtnLetter_Click(object sender, CommandEventArgs e)
	{
        if (e.CommandName.Equals("AlphabetSelect"))
        {
            ChangeODSSelection("FindUsersByName", "usernameToMatch");

            char letter = ((string)e.CommandArgument)[0];
            hfSearchQuery.Value = letter.ToString() + "%"; //wildcard character added
        }
	}

    protected void btnDeleteUser_Click(object sender, CommandEventArgs e)
    {
        if (e.CommandName.Equals("DeleteUser"))
        {
            if (e.CommandArgument != null)
            {
                Membership.DeleteUser(e.CommandArgument.ToString(), false);
            }
        }
        gvUsers.DataBind();
    }

    protected void lbtnEditUser_Click(object sender, CommandEventArgs e)
    {
        if (e.CommandName.Equals("EditUser"))
        {
            string redirectString = "~/ProfileAdministration/EditUser.aspx" + "?username=" + e.CommandArgument.ToString();
            Response.Redirect(redirectString);
        }
    }

	protected void lbtnAll_Click(object sender, EventArgs e)
	{
        ChangeODSSelection("FindUsersByName", "usernameToMatch");
        hfSearchQuery.Value = "%";
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        if (ddlSearchBy.SelectedValue == "UserName")
        {
            ChangeODSSelection("FindUsersByName", "usernameToMatch");
        }
        else
        {
            ChangeODSSelection("FindUsersByEmail", "emailToMatch");
        }

        if (tbSearchString.Text != string.Empty)
        {
            string searchString = tbSearchString.Text.Replace("%", "&"); //non username character
            searchString = searchString.Replace('*', '%');
            hfSearchQuery.Value = searchString;
        }
    }
    
    #endregion 

    #region Methods

    private List<char> GetAlphabetList()
	{
		const int letterCount = 26;
		List<char> alphabet = new List<char>(letterCount);

		alphabet.AddRange(
			new char[]{ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'V', 'W', 'X', 'Y', 'Z' });
		return alphabet; 
	}
    
    private void ChangeODSSelection(string method, string parameterName)
    {
        odsMembershipUsers.SelectMethod = method;
        odsMembershipUsers.SelectParameters.Clear();
        odsMembershipUsers.SelectParameters.Add(
            new ControlParameter(parameterName, "hfSearchQuery", "Value"));
    }

    #endregion 
}
